package com.mjduan.project.badExample;

/**
 * Why this class is bad, because all the rules will be manipulated in
 * the openWindow or closeWindow method body.
 * Besides, I think there is much duplicated code in openWindow and
 * closeWindow method body. And there are lots of logical branch
 * so if you use sonarqube to analysis your code, the coverage is
 * declining rapidly.
 *
 * Hans 2017-09-16 20:09
 */
public class AutomaticWindow {
    final static int CLOSED = 0;
    final static int OPEN = 1;

    int state = CLOSED;

    public void openWindow() {
        if (state == OPEN) {
            System.out.println("W is already open");
        } else if (state == CLOSED) {
            System.out.println("Opening window");
            state = OPEN;
        }
    }

    public void closeWindow() {
        if (state == OPEN) {
            System.out.println("Closing window");
            state = CLOSED;
        } else if (state == CLOSED) {
            System.out.println("Window is already closed");
        }
    }

    @Override
    public String toString() {
        return state == OPEN ? "window is open" : "window is closed";
    }

}
